﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.OA.Workflow.WorkflowInstanceStepExecutionRecord;
using Dapper;

namespace BCData.OA.Workflow.WorkflowInstanceStepExecutionRecord
{
    public class WorkflowInstanceStepExecutionRecordData: IWorkflowInstanceStepExecutionRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT WorkflowInstanceStepExecutionRecordId,WorkflowActivityInstanceNo,TaskStep,TaskMethodTitle,TaskServiceName,TaskMethodName,TaskMethodParameters,
                                            Result
                                        FROM OA_WorkflowInstanceStepExecutionRecord";
        public WorkflowInstanceStepExecutionRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<WorkflowInstanceStepExecutionRecordEntity> Add(WorkflowInstanceStepExecutionRecordEntity workflowInstanceStepExecutionRecordEntity)
        {
            string sql = SqlCommands.GetInsertSql("OA_WorkflowInstanceStepExecutionRecord", new string[] { "WorkflowActivityInstanceNo", "TaskStep", "TaskMethodTitle", "TaskServiceName",
                                "TaskMethodName","TaskMethodParameters","Result" });
            sql = string.Format("{0} {1} WHERE WorkflowInstanceStepExecutionRecordId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceNo", workflowInstanceStepExecutionRecordEntity.WorkflowActivityInstanceNo, DbType.AnsiString, size: 50);
            parameters.Add("?TaskStep", workflowInstanceStepExecutionRecordEntity.TaskStep, DbType.Int32, size: 10);
            parameters.Add("?TaskMethodTitle", workflowInstanceStepExecutionRecordEntity.TaskMethodTitle, DbType.AnsiString, size: 100);
            parameters.Add("?TaskServiceName", workflowInstanceStepExecutionRecordEntity.TaskServiceName, DbType.AnsiString, size: 200);
            parameters.Add("?TaskMethodName", workflowInstanceStepExecutionRecordEntity.TaskMethodName, DbType.AnsiString, size: 100);
            parameters.Add("?TaskMethodParameters", workflowInstanceStepExecutionRecordEntity.TaskMethodParameters, DbType.AnsiString, size: 1000);
            parameters.Add("?Result", workflowInstanceStepExecutionRecordEntity.Result, DbType.String);

            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowInstanceStepExecutionRecordEntity>(sql, parameters, databaseContext.Transaction);
        }
    }
}
